/*
 * @Author: Marte
 * @Date:   2016-11-01 09:35:09
 * @Last Modified by:   Marte
 * @Last Modified time: 2016-11-04 17:23:05
 */

'use strict';

var config = require('./config');
var gulp = require('gulp');
var browserSync = require('browser-sync');
var path = require('path');
var reload = browserSync.reload;
var nodemon = require('gulp-nodemon');
var imagemin = require('gulp-imagemin'), //图片压缩
    pngcrush = require('imagemin-pngcrush'),
    minifycss = require('gulp-minify-css'), //css压缩
    jshint = require('gulp-jshint'), //js检测
    uglify = require('gulp-uglify'), //js压缩
    concat = require('gulp-concat'), //文件合并
    rename = require('gulp-rename'), //文件更名
    notify = require('gulp-notify'); //提示信息
// var jshint = require('gulp-jshint');
// var stylish = require('jshint-stylish');

//gulp入口

var nodemonConfig = {
    script: 'bin/www',
    ext: 'js jade',
    ignore: [
        'node_modules/**/*',
        'client/**/*',
        'bower_components/**/*',
        'resource/**/*',
        'resource_dev/**/*'
    ],
    env: {
        NODE_ENV: 'development'
    }
};

var port = process.env.PORT || 3100;
gulp.task('browser-sync', ['nodemon'], function(done) {
    browserSync({
        proxy: 'localhost:' + port, //项目端口
        port: 3200, // 浏览器访问端口
        notify: true
    }, done);
});

gulp.task('nodemon', function(cb) {
    var called = false;
    return nodemon(nodemonConfig)
        .on('start', function() {
            if (!called) {
                called = true;
                cb();
            }
        })
        .on('restart', function() {
            setTimeout(function() {
                console.log('-------- 重启 --------');
                reload({
                    stream: false
                });
            }, 1000);
        });
});

var clientPubDevDir = path.join(process.cwd(), '/public_dev');
var clientPubDir = path.join(process.cwd(), '/public');

// 压缩图片,前台
gulp.task('pubimg', function() {
    return gulp.src(path.join(clientPubDevDir, '/images/**'))
        .pipe(imagemin({
            progressive: true,
            svgoPlugins: [{
                removeViewBox: false
            }],
            use: [pngcrush()]
        }))
        .pipe(gulp.dest(path.join(clientPubDir, '/images/')));
    //.pipe(notify({ message: 'img task ok' }));
});

// 压缩css,前台
gulp.task('pubcss', function() {
    return gulp.src(path.join(clientPubDevDir, '/css/**/*.css'))
        //.pipe(concat('main.css')) //合并为main.css
        //.pipe(rename({ suffix: '.min' })) //重命名为main.min.css
        .pipe(minifycss()) //压缩
        .pipe(gulp.dest(path.join(clientPubDir, '/css/')));
    //.pipe(notify({ message: 'css task ok' }));
});


// 压缩js文件,前台
gulp.task('pubjs', function() {
    return gulp.src(path.join(clientPubDevDir, '/js/**/*.js'))
        //.pipe(concat('all.js')) //合并
        //.pipe(rename({ suffix: '.min' })) //重命名
        .pipe(uglify({ mangle: true })) //压缩换行和空格,压缩变量命名
        .pipe(gulp.dest(path.join(clientPubDir, '/js/')));
    //.pipe(notify({ message: 'js task ok' }));
});

var scripts = [
    'public_dev/**/*.js'
];
//检查js
gulp.task('lint', function() {
    return gulp.src(scripts)
        .pipe(jshint())
        .pipe(jshint.reporter('jshint-stylish'));
});

// gulp.task('watch', function() {
//   gulp.watch(scripts, ['lint']);
// });

//gulp.task('default', ['browser-sync']);
gulp.task('default', function() {
    gulp.run('pubimg', 'pubcss', 'pubjs',
        'lint');
    gulp.watch(path.join(clientPubDevDir, '/images/**/*.*'), ['pubimg']);
    gulp.watch(path.join(clientPubDevDir, '/css/**/*.css'), ['pubcss']);
    gulp.watch(path.join(clientPubDevDir, '/js/**/*.js'), ['pubjs']);
    gulp.run('browser-sync');
});
